/**
 * <p>Title: Cell Class</p>
 * <p>Description: CS 343 Project</p>
 * @author Quintin Stolpe
 * @email quintin.stolpe@my.uwrf.edu
 * @date Dec 3 2012
*/

package maze;

//Ghost class
public class Ghost
{
	private int[][] myMaze;

	private int x, y;
	private int dirX = 1;
	private int dirY = 0;
	private boolean lastChoiceX = false;

	private int speed = 6;
	private int size = 1;
	
	private Edge myEdge = null;
	
	
	
	public int getX()
	{
		return x;
	}
	public int getY()
	{
		return y;
	}
	
	//Method to move the Ghost
	public void move(int pacX, int pacY, Graph graph)
	{
		int mazeX = x / size;
		int mazeY = y / size;

		if (myEdge == null)
		{
			
			myEdge = graph.getEdge(mazeX, mazeY);
			if (myEdge.contains(mazeX - 1, mazeY))
			{
				dirX = -1;
				dirY = 0;
			}
			else if (myEdge.contains(mazeX + 1, mazeY))
			{
				dirX = 1;
				dirY = 0;
			}
			else if (myEdge.contains(mazeX, mazeY - 1))
			{
				dirX = 0;
				dirY = -1;
			}
			else if (myEdge.contains(mazeX, mazeY + 1))
			{
				dirX = 0;
				dirY = 1;
			}
			else
			{
				System.out.println("error in Ghost.move");
			}
		}

		if (x % size == 0 && y % size == 0)
		{
			
			Node node = graph.getNode(mazeX, mazeY);

			if (node != null)
			{
				
				node.findPathTo(pacX, pacY, 2);
				myEdge = node.bestEdge;
				if (myEdge.contains(mazeX - 1, mazeY))
				{
					dirX = -1;
					dirY = 0;
				}
				else if (myEdge.contains(mazeX + 1, mazeY))
				{
					dirX = 1;
					dirY = 0;
				}
				else if (myEdge.contains(mazeX, mazeY - 1))
				{
					dirX = 0;
					dirY = -1;
				}
				else if (myEdge.contains(mazeX, mazeY + 1))
				{
					dirX = 0;
					dirY = 1;
				}
				else
				{
					System.out.println("error in Ghost.move");
				}

			}
			else if (
				myEdge != null && !myEdge.contains(mazeX + dirX, mazeY + dirY))
			{
				
				if (dirX != 0)
				{
					if (myEdge.contains(mazeX, mazeY - 1))
					{
						dirX = 0;
						dirY = -1;
					}
					else if (myEdge.contains(mazeX, mazeY + 1))
					{
						dirX = 0;
						dirY = 1;
					}
					else
					{
						myEdge.contains(mazeX + dirX, mazeY + dirY);
						System.out.println("Error in dirX!=0");
						dirX = -dirX;
						dirY = 0;
					}
				}
				else if (dirY != 0)
				{
					if (myEdge.contains(mazeX - 1, mazeY))
					{
						dirX = -1;
						dirY = 0;
					}
					else if (myEdge.contains(mazeX + 1, mazeY))
					{
						dirX = 1;
						dirY = 0;
					}
					else
					{
						myEdge.contains(mazeX + dirX, mazeY + dirY);
						System.out.println("Error in dirY!=0");
						dirX = 0;
						dirY = -dirY;
					}
				}
				else
				{

					System.out.println(
						"Error navigating segment transition in edgein Ghost.mvoe");
				}
			

			}
		}

		x += dirX * speed;
		y += dirY * speed;

	}

	public void setMaze(int[][] maze)
	{
		myMaze = new int[maze.length][maze[0].length];
		for (int i = 0; i < myMaze.length; i++)
		{
			for (int j = 0; j < myMaze[i].length; j++)
			{
				if (maze[i][j] == Maze.WALL)
				{
					myMaze[i][j] = Maze.WALL;
				}
				else
				{
					myMaze[i][j] = Maze.EMPTY_SQUARE;
				}
			}
		}
	}
	public void setXY(int anX, int aY)
	{
		x = anX * size;
		y = aY * size;
		dirX = 1;
		dirY = 1;

		myEdge = null;
		

	}

	public int getSpeed()
	{
		return speed;
	}

	
	public void setSpeed(int i)
	{
		speed = i;
	}


	public int getSize()
	{
		return size;
	}


	public void setSize(int i)
	{
		size = i;
	}

}
